
cd "/Users/annettetov/"
global in="~/Dropbox/gift/solar/BoondDynamicPricing/Data_MAIN/Analysis"
global survey="~/Dropbox/gift/solar/BoondDynamicPricing/Data_annual"
global outM="~/Dropbox/gift/solar/BoondDynamicPricing/Manuscript"

capture log close
clear all
set more off

*	************************************************************************
* 	File-Name: 	replication_EP.do
*	Date:  		06/04/2018
*	Author: 	Semee Yoon
*	Data Used:  master_manuscript_final.dta / powercheck.dta
*	Output		None
*	Purpose:   	.do file to replicate the findings in 
* 				"An Evaluation of Dynamic Electricity Pricing for Solar Micro-Grids in Rural India"
*	************************************************************************

*	************************************************************************
* 	0. Setting up the data
*	************************************************************************

* Change this link to your working directory where you want to work.
cd "/Users/serene/"
global inSHS="~/Dropbox/gift/solar/shs_j_s/DataCode_EXPERIMENTAL/"
global outSHS="~/Dropbox/gift/solar/shs_j_s/FINAL"

* Change this link to your working directory where the dataset is located.
cd "/Users/annettetov/"
global in="~/Dropbox/gift/solar/BoondDynamicPricing/Data_MAIN/Analysis"
global survey="~/Dropbox/gift/solar/BoondDynamicPricing/Data_annual"
global outM="~/Dropbox/gift/solar/BoondDynamicPricing/Manuscript"

* Change this link to your working directory where the dataset is located.
use $in/master_manuscript_final, clear


*	************************************************************************
* 	1. Summary statistics
*	************************************************************************

*** Table S1: Covariate Balance:Balance statistics of household characteristics for the sample in the static pricing mode and the dynamic pricing mode.

use $in/master_manuscript_final, clear

local hhchar female HHage HHsize married  q_109_children_below_18age q_110_children_school q_115_readhindi caste_dum1 caste_dum2 educ_dum1 educ_dum2 educ_dum3 educ_dum4 educ_dum5 job_dum1 job_dum2 q_117_own_business_or_not q_118_rationcard q_119_monthlyexpenses q_121_monthlysaving kero_PDS mainlight_dum1 mainlight_dum2

eststo clear
estpost summ `hhchar' if randomization==0
eststo A
estpost summ `hhchar' if randomization==1
eststo B
esttab A B  using $outM/Tables/su_hhchar_treat1.tex, replace booktabs collabels(none) cells(mean(fmt(a2)) sd(par fmt(a2))) mtitle("Static Pricing" "Dynamic Pricing") unstack label nonum noobs nogaps nodepvar star(* 0.10 ** 0.05 *** 0.01)

* column of t-test
eststo clear
foreach var of local hhchar {
	eststo: reg `var' randomization, vce(cluster weeknumber)
}
esttab  using $outM/Tables/su_hhchar_ttest.tex, keep(randomization) replace label nogaps nomtitle star(* 0.10 ** 0.05 *** 0.01) se b(%9.3f) ar2(%9.3f) obslast nomtitle booktabs

*** Table S2: Outcomes by pricing treatment

local weeklyresp w101HH w202elec_app w201elec_exp light_pday fan_day mobile_day light_prob_howmany fan_prob_howmany mobile_prob_howmany price_elec difficulty satisfaction watthour

eststo clear
estpost summ `weeklyresp' if randomization==0
eststo A
estpost summ `weeklyresp' if randomization==1
eststo B
esttab A B  using $outM/Tables/su_treatment.tex, replace booktabs collabels(none) cells(mean(fmt(a2)) sd(par fmt(a2))) mtitle("Static Pricing" "Dynamic Pricing") unstack label nonum noobs nogaps nodepvar star(* 0.10 ** 0.05 *** 0.01)

*** Table S3: Summary statistics of participating households.

eststo clear
estpost summ `hhchar' 
eststo A
esttab A using $outM/Tables/su_hhchar.tex, replace booktabs collabels(none) cells("mean(fmt(a2)) sd(par fmt(a2)) min max") mtitle("Mean" "SD" "Min" "Max") unstack label nonum noobs nogaps nodepvar star(* 0.10 ** 0.05 *** 0.01)


*	************************************************************************
* 	2. Main analysis
*	************************************************************************

global ctrl = "female HHage HHsize q_110_children_school q_115_readhindi caste_dum* educ_dum* q_119_monthlyexpenses q_121_monthlysaving"
global ctrl_log = "female HHage HHsize q_110_children_school q_115_readhindi caste_dum* educ_dum* log_expenditure log_savings"

egen hhid = group(deviceid)
egen hab_hhid= group(habitation_id hhid)
egen hab_week = group(habitation_id weeknumber)

*** Table 1: Effect of dynamic pricing on electricity consumption and use. 

gen byte constant = 1
eststo clear
eststo: 	reghdfe watthour randomization  , absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe watthour randomization  i.habitation_id i.weeknumber , absorb(constant) vce(cluster hab_week) 
eststo: 	reghdfe light_pday   randomization  ,  absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe light_pday   randomization  i.habitation_id i.weeknumber ,  absorb(constant) vce(cluster hab_week)  
eststo: 	reghdfe fan_day  randomization ,  absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe fan_day  randomization  i.habitation_id i.weeknumber ,  absorb(constant) vce(cluster hab_week) 
eststo: 	reghdfe mobile_day  randomization  ,  absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe mobile_day  randomization i.habitation_id i.weeknumber ,  absorb(constant) vce(cluster hab_week)  
estadd ysumm: *
esttab using $outM/Tables/ITT_h1_noc_n_check.tex, keep(randomization) indicate("Habitation FE = _Ihab*" "Week FE = _Iweek*" ) replace nogaps nomtitle label booktabs star(* 0.10 ** 0.05 *** 0.01) b(%9.3f)  se  stats(r2_a N ymean, labels( "Adjusted \(R^{2}\)" "Observations" "Mean of Dep Var") fmt(3 0 3)) obslast

*** Table S4: Effect of dynamic pricing on electricity consumption and use with control variables. 

eststo clear
eststo: 	reghdfe watthour randomization $ctrl_log , absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe watthour randomization $ctrl_log i.habitation_id i.weeknumber , absorb(constant) vce(cluster hab_week) 
eststo: 	reghdfe light_pday   randomization $ctrl_log ,  absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe light_pday   randomization $ctrl_log i.habitation_id i.weeknumber ,  absorb(constant) vce(cluster hab_week)  
eststo: 	reghdfe fan_day  randomization $ctrl_log,  absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe fan_day  randomization $ctrl_log i.habitation_id i.weeknumber ,  absorb(constant) vce(cluster hab_week) 
eststo: 	reghdfe mobile_day  randomization $ctrl_log ,  absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe mobile_day  randomization $ctrl_log i.habitation_id i.weeknumber ,  absorb(constant) vce(cluster hab_week)  
estadd ysumm: *
esttab using $outM/Tables/ITT_h1_hhwk_check.tex, keep(randomization female HHsize q_110_children_school q_115_readhindi  educ_dum5 log_expenditure log_savings ) indicate("Habitation FE = _Ihab*" "Week FE = _Iweek*" ) replace nogaps nomtitle label booktabs star(* 0.10 ** 0.05 *** 0.01)  b(%9.3f)  se  stats(r2_a N ymean, labels( "Adjusted \(R^{2}\)" "Observations" "Mean of Dep Var") fmt(3 0 3))  obslast

*** Table 2: Effect of dynamic pricing on self-reported technical problems. 

eststo clear
gen byte constant = 1
eststo: 	reghdfe light_prob_howmany randomization  , absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe light_prob_howmany randomization  i.habitation_id i.weeknumber , absorb(constant) vce(cluster hab_week) 
eststo: 	reghdfe fan_prob_howmany   randomization  , absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe fan_prob_howmany   randomization  i.habitation_id i.weeknumber , absorb(constant)  vce(cluster hab_week) 
eststo: 	reghdfe mobile_prob_howmany  randomization , absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe mobile_prob_howmany   randomization  i.habitation_id i.weeknumber , absorb(constant)  vce(cluster hab_week) 
estadd ysumm: *
esttab using $outM/Tables/ITT_h2_noc_n.tex, keep(randomization) indicate("Habitation FE = _Ihab*" "Week FE = _Iweek*" ) replace nogaps nomtitle label booktabs star(* 0.10 ** 0.05 *** 0.01)  b(%9.3f)  se  stats(r2_a N ymean, labels( "Adjusted \(R^{2}\)" "Observations" "Mean of Dep Var") fmt(3 0 3))  obslast

*** Table S5: Effect of dynamic pricing on self-reported technical problems with control variables

eststo clear
eststo: 	reghdfe light_prob_howmany randomization $ctrl_log , absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe light_prob_howmany randomization $ctrl_log i.habitation_id i.weeknumber , absorb(constant) vce(cluster hab_week) 
eststo: 	reghdfe fan_prob_howmany   randomization $ctrl_log , absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe fan_prob_howmany   randomization $ctrl_log i.habitation_id i.weeknumber , absorb(constant)  vce(cluster hab_week) 
eststo: 	reghdfe mobile_prob_howmany  randomization $ctrl_log, absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe mobile_prob_howmany  randomization $ctrl_log i.habitation_id i.weeknumber , absorb(constant)  vce(cluster hab_week) 
estadd ysumm: *
esttab using $outM/Tables/ITT_h2_hhwk.tex, keep(randomization female HHsize q_110_children_school q_115_readhindi  educ_dum5 log_expenditure log_savings ) indicate("Habitation FE = _Ihab*" "Week FE = _Iweek*" ) replace nogaps nomtitle label booktabs star(* 0.10 ** 0.05 *** 0.01) b(%9.3f)  se  stats(r2_a N ymean, labels( "Adjusted \(R^{2}\)" "Observations" "Mean of Dep Var") fmt(3 0 3)) obslast

*** Table 3: Effect of dynamic pricing on sub jective satisfaction

eststo clear
gen byte constant = 1
eststo: 	reghdfe price_elec randomization  , absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe price_elec randomization  i.habitation_id i.weeknumber , absorb(constant) vce(cluster hab_week) 
eststo: 	reghdfe difficulty  randomization  , absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe difficulty   randomization  i.habitation_id i.weeknumber , absorb(constant)  vce(cluster hab_week) 
eststo: 	reghdfe satisfaction   randomization , absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe satisfaction    randomization  i.habitation_id i.weeknumber , absorb(constant)  vce(cluster hab_week) 
estadd ysumm: *
esttab using $outM/Tables/ITT_h3_noc_n_check.tex, keep(randomization) indicate("Habitation FE = _Ihab*" "Week FE = _Iweek*" ) replace nogaps nomtitle label booktabs star(* 0.10 ** 0.05 *** 0.01)  b(%9.3f)  se  stats(r2_a N ymean, labels( "Adjusted \(R^{2}\)" "Observations" "Mean of Dep Var") fmt(3 0 3))  obslast


*** Table S6: Effect of dynamic pricing on subjective satisfaction with control variables

eststo clear
eststo: 	reghdfe price_elec randomization $ctrl_log  , absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe price_elec randomization $ctrl_log  i.habitation_id i.weeknumber , absorb(constant) vce(cluster hab_week) 
eststo: 	reghdfe difficulty  randomization $ctrl_log  , absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe difficulty  randomization $ctrl_log  i.habitation_id i.weeknumber , absorb(constant)  vce(cluster hab_week) 
eststo: 	reghdfe satisfaction   randomization $ctrl_log , absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe satisfaction   randomization $ctrl_log  i.habitation_id i.weeknumber , absorb(constant)  vce(cluster hab_week) 
estadd ysumm: *
esttab using $outM/Tables/ITT_h3_hhwk.tex, keep(randomization female HHsize q_110_children_school q_115_readhindi  educ_dum5 log_expenditure log_savings ) indicate("Habitation FE = _Ihab*" "Week FE = _Iweek*") replace nogaps nomtitle label booktabs star(* 0.10 ** 0.05 *** 0.01)  b(%9.3f)  se  stats(r2_a N ymean, labels( "Adjusted \(R^{2}\)" "Observations" "Mean of Dep Var") fmt(3 0 3))  obslast

*** Table 4: Effect of dynamic pricing on subjective satisfaction

preserve
collapse SC_all SC_sun randomization, by(habitation_id weeknumber)
egen hab_week = group(habitation_id weeknumber)
eststo clear
gen byte constant = 1
eststo: 	reghdfe SC_all  randomization  , absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe SC_all  randomization  i.habitation_id i.weeknumber , absorb(constant) vce(cluster hab_week) 
eststo: 	reghdfe SC_sun randomization  , absorb(constant) vce(cluster hab_week) 
eststo: xi: reghdfe SC_sun   randomization  i.habitation_id i.weeknumber , absorb(constant)  vce(cluster hab_week) 
estadd ysumm: *
esttab using $outM/Tables/ITT_h4_noc_n.tex, keep(randomization) indicate("Habitation FE = _Ihab*" "Week FE = _Iweek*" ) replace nogaps nomtitle label booktabs star(* 0.10 ** 0.05 *** 0.01) b(%9.3f)  se stats(r2_a N ymean ysd ymin ymax, labels( "Adjusted \(R^{2}\)" "Observations" "Mean of Dep Var" "St. Dev of Dep Var" "Min of Dep Var" "Max of Dep Var" ) fmt(3 0 3 3 3 3))  obslast 
restore

*	************************************************************************
* 	3. Additional analysis
*	************************************************************************

*** Table S7: Summary of system interruptions by pricing treatment

eststo clear
estpost summ all powergap if randomization==0 
eststo A
estpost summ all powergap if randomization==1 
eststo B
estpost ttest all powergap, by(randomization)
esttab C
esttab using $outM/Tables/powerout_new.tex, label booktabs cells(mean(fmt(a2)) sd(par fmt(a2))) mtitle("Static" "Dynamic") nonum replace 

*	************************************************************************
* 	4. Graphs
*	************************************************************************

*** Figure 1: Average weekly watt-hours by treatment. 
local f0 = "red"
local f1 = "blue"

bys weeknumber: egen mean_watt0 = mean(watthour) if randomization==0
bys weeknumber: egen mean_watt1 = mean(watthour) if randomization==1

label var mean_watt0 "Static Pricing"
label var mean_watt1 "Dynamic Pricing"
twoway (scatter mean_watt0 weeknumber , ytitle("Average weekly electricity use (in Watt-hour)") yscale(r(0 200)) msymbol(Oh) mcolor(`f0')) (scatter mean_watt1  weeknumber , msymbol(Oh) mcolor(`f1')) , graphregion(color(white)) bgcolor(white)
graph export "$outM/Figures/watthour.pdf", replace



